<?php

namespace app\proxy\controller\setting;

use app\Request;
use app\proxy\controller\Controller;
use app\proxy\model\setting\DicModel;

/**
 * 商城设置管理
 */
class Dic extends Controller
{
    /**
     * 商城设置列表
     */
    public function index(Request $request)
    {
        $model    = new DicModel();
        $param    = $request->param();
        $regions  = $this->getRegionIds();
        $list     = $model->getList($param, $regions, $this->getProxyId());
        $category = config('dic');
        // 数量
        $category_count = [
            'using'   => $model->getProxyTotal(['audit_status' => 1], $regions),
            'audit'   => $model->getProxyTotal(['audit_status' => 0], $regions),
            'no_pass' => $model->getProxyTotal(['audit_status' => 2], $regions),
        ];
        return $this->renderSuccess('', compact('list', 'category', 'category_count'));
    }

    /**
     * 创建
     * @param Request $request
     * @return \think\response\Json
     * @Author joey
     * @date 2024/3/26 17:55
     */
    public function create(Request $request)
    {
        try {
            $param                 = $request->param();
            $param['audit_status'] = 0;
            // 新增记录
            $result = (new DicModel)->add($param, $this->getProxyId());
            return $this->renderSuccess('操作成功', $result);
        } catch (\Exception $exception) {
            return $this->renderError($exception->getMessage());
        }
    }

    /**
     * 编辑
     * @param Request $request
     * @return \think\response\Json
     * @Author joey
     * @date 2024/3/26 17:55
     */
    public function edit(Request $request)
    {
        $where = $request->param();
        // 详情
        $model = DicModel::one($where);
        return $this->renderSuccess('', compact('model'));

    }

    /**
     * 更新
     * @param Request $request
     * @return \think\response\Json
     * @Author joey
     * @date 2024/3/26 17:55
     */
    public function update(Request $request)
    {
        $where = $request->param();
        // 详情
        $model = DicModel::find($where['id']);
        if ($model->isEmpty() || $model['proxy_id'] != $this->getProxyId()) {
            return $this->renderError('暂无权限操作');
        }
        // 更新记录
        if ($model->edit(array_merge($where, ['audit_status' => 0]), $this->getProxyId())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除
     * @param Request $request
     * @return \think\response\Json
     * @Author joey
     * @date 2024/3/26 17:55
     */
    public function delete(Request $request)
    {
        $params = $request->param();
        $model  = DicModel::detail($params['id']);
        if ($model->isEmpty() || $model['proxy_id'] != $this->getProxyId()) {
            return $this->renderError('暂无权限操作');
        }
        // 修改记录
        DicModel::deleteData($params);
        return $this->renderSuccess('删除成功');
    }

    /**
     * 分类
     * @return \think\response\Json
     * @Author joey
     * @date 2024/3/26 17:55
     */
    public function category()
    {
        return $this->renderSuccess('', config('dic'));
    }
}
